﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.Client.TVViewer.
//TV4Home.Client.TVViewer is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.Client.TVViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU Lesser General Public License along with TV4Home.Client.TVViewer. If not, see http://www.gnu.org/licenses/.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;

namespace TV4Home.Client.Desktop.TVViewer.Pages
{
    /// <summary>
    /// Interaktionslogik für Schedules.xaml
    /// </summary>
    public partial class Schedules : Page
    {
        #region Attribute


        ITVEInteraction tvWebClient;
        List<WebSchedule> _schedules = null;

        #endregion

        public Schedules()
        {
            InitializeComponent();

        }
        public void FillPage()
        {
            try
            {
                if (tvWebClient == null)
                {
                    ConnectToService();
                }
                //Get all Schedules

                _schedules = tvWebClient.GetSchedules();
       
                if (_schedules != null)
                {
                    
                        lbRecordings.ItemsSource = _schedules;
                        lbRecordings.Items.Refresh();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Outer Exception: FillPage " + ex.ToString());
            }
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.ServerName != "")
            {
                FillPage();
            }
        }

        private void lbRecordings_Selected(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is ListBoxItem)
            {

                ListBoxItem item = (ListBoxItem)e.OriginalSource;


                if (item.Content is WebSchedule)
                {
                    if (tvWebClient == null)
                    {
                        ConnectToService();
                    }
                    WebSchedule schedule = (WebSchedule)item.Content;
                    WebChannelBasic channel = tvWebClient.GetChannelBasicById(schedule.IdChannel);


                    List<WebProgramBasic> program = tvWebClient.GetProgramsBasicForChannel(channel.IdChannel, schedule.StartTime, schedule.EndTime);

                    tbProgramTitle.Text = program[0].Title;
                    tbProgramDescription.Text = program[0].Description;       
                    ChangeVisibility(System.Windows.Visibility.Hidden);
                   




                }

            }
        }
        public void ConnectToService()
        {
            try
            {
                if (Properties.Settings.Default.IsIntranet)
                {
                    try
                    {
                        tvWebClient = ChannelFactory<ITVEInteraction>.CreateChannel(new BasicHttpBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress(String.Format(Properties.Settings.Default.IntranetUrl, Properties.Settings.Default.ServerName)));
                    }
                    catch (CommunicationException ex)
                    {
                        tvWebClient = ChannelFactory<ITVEInteraction>.CreateChannel(new BasicHttpBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress(String.Format(Properties.Settings.Default.IntranetUrl, Properties.Settings.Default.ServerName)));

                    }
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                string sMessageBoxText = "Verbindung konnte nicht hergestellt werden. Erneut versuchen?";
                string sCaption = "TVViewer";

                MessageBoxButton btnMessageBox = MessageBoxButton.YesNo;
                MessageBoxImage icnMessageBox = MessageBoxImage.Warning;

                MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);


                // Process message box results

                switch (rsltMessageBox)
                {

                    case MessageBoxResult.Yes:
                        ConnectToService();

                        break;
                    case MessageBoxResult.No:

                        break;
                }
            }

        }
        private void ChangeVisibility(Visibility visible)
        {

            switch (visible)
            {
                case System.Windows.Visibility.Hidden:
                    complete_overlay.Visibility = System.Windows.Visibility.Visible;
                    details_overlay.Visibility = System.Windows.Visibility.Visible;
                    btnCloseDetails.Visibility = System.Windows.Visibility.Visible;
                    tbProgramDescription.Visibility = System.Windows.Visibility.Visible;
                    tbProgramTitle.Visibility = System.Windows.Visibility.Visible;
                    lblDescription.Visibility = System.Windows.Visibility.Visible;
                    lblTitle.Visibility = System.Windows.Visibility.Visible;
                    topRectangle.Visibility = System.Windows.Visibility.Visible;
                    btnCancelSchedule.Visibility = System.Windows.Visibility.Visible;
                    break;
                case System.Windows.Visibility.Visible:
                    complete_overlay.Visibility = System.Windows.Visibility.Hidden;
                    details_overlay.Visibility = System.Windows.Visibility.Hidden;
                    btnCloseDetails.Visibility = System.Windows.Visibility.Hidden;
                    tbProgramDescription.Visibility = System.Windows.Visibility.Hidden;
                    tbProgramTitle.Visibility = System.Windows.Visibility.Hidden;
                    lblDescription.Visibility = System.Windows.Visibility.Hidden;
                    lblTitle.Visibility = System.Windows.Visibility.Hidden;
                    topRectangle.Visibility = System.Windows.Visibility.Hidden;
                    btnCancelSchedule.Visibility = System.Windows.Visibility.Hidden;
                    break;


            }
        }
        private void complete_overlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            _schedules = tvWebClient.GetSchedules();
            FillPage();
            ChangeVisibility(System.Windows.Visibility.Visible);

        }

        private void btnCancelSchedule_Click(object sender, RoutedEventArgs e)
        {
          
                if (lbRecordings.SelectedItems[0] is WebSchedule)
                {
                    WebSchedule schedule = lbRecordings.SelectedItems[0] as WebSchedule;
                    if (tvWebClient == null)
                    {
                        ConnectToService();
                    }
                    tvWebClient.DeleteSchedule(schedule.IdSchedule);                
                    _schedules = tvWebClient.GetSchedules();
                    FillPage();
                    ChangeVisibility(System.Windows.Visibility.Visible);
                }

            
        }
        private void btnCloseDetails_Click(object sender, RoutedEventArgs e)
        {
            complete_overlay_MouseLeftButtonDown(this, null);
        }


    }
    [ValueConversion(typeof(object), typeof(object))]
    public class DateTimeConverter : IValueConverter
    {

        public object Convert(object value, Type targetType,
                 object parameter, System.Globalization.CultureInfo culture)
        {
            if (culture == null)
            {
                culture = new System.Globalization.CultureInfo("de-DE");
                return ((DateTime)value).ToString("d.M HH:mm", culture);
            }
            else
            {
                return ((DateTime)value).ToString("d.M HH:mm", culture);
            }
        }
        public object ConvertBack(object value, Type targetType,
                 object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
